package sky.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import sky.domain.Plane;
import sky.service.CountryService;
import sky.service.PlaneService;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 21.04.13
 * Time: 23:31
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PlaneController {

    @Autowired
    private PlaneService planeService;

    @RequestMapping(value = "/addPlane", method = RequestMethod.POST)
    public String addPlane(@ModelAttribute("plane") Plane plane, Map<String, Object> map) {

        planeService.addPlane(plane);
        return "redirect:/planes";
    }

    @RequestMapping(value = "planes/edit/*", method = RequestMethod.POST)
    public String editPlane(@ModelAttribute("plane") Plane plane, BindingResult result) {

        planeService.editPlane(plane);

        return "redirect:/index";
    }

    @RequestMapping(value = "planes/edit/{planeId}", method = RequestMethod.GET)
    public String editPlane(@PathVariable("planeId") Integer planeId, Map<String, Object> map) {

        map.put("plane", planeService.getPlaneById(planeId));
        return "editPlane";
    }

    @RequestMapping(value = "planes/delete/{planeId}")
    public String deleteContact(@PathVariable("planeId") Integer planeId) {

        planeService.removePlane(planeId);

        return "redirect:/planes";
    }

    @RequestMapping(value = "/planes", method = RequestMethod.GET)
    public String mainPlane(Map<String, Object> map) {
        map.put("planeList", planeService.listPlane());
        map.put("NPlane", new Plane());
        return "planes";
    }

    @RequestMapping(value = "/plane", method = RequestMethod.GET)
    public String planeInfo(@RequestParam("id") Integer planeId, Map<String, Object> map) {
        map.put("plane", planeService.getPlaneById(planeId));
        return "plane";
    }
}
